package com.wkk.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 * 功能：实现在未登录状态下，不能执行除登录注册以外的操作
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getSession().getAttribute("nowUser") != null) {

            return true;
        } else {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset = UTF-8");
            response.getWriter().println("<script language='javascript'>alert('若想使用其他功能，请先登录!')"
                    + ";top.location='/contact-us';</script>");
            return false;
        }


    }
}
